/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.clebersantos.bean;

import br.com.clebersantos.pojos.Ingredientes;
import br.com.clebersantos.pojos.Produto;
import br.com.clebersantos.util.HibernateUtil;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Jurema
 */
@ManagedBean
@FacesConverter
public class ConverterBean implements Converter {

    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String string) {

        System.out.println("Recebendo a String: " + string);
            
            Session em = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = em.beginTransaction();
            Criteria cri = em.createCriteria(Produto.class)
                    .add(Restrictions.eq("nome", string));

            Produto p = new Produto();
            p = (Produto) cri.uniqueResult();

            System.out.println("mandando o Objeto: " + p.getNome() + " | " + p.getPrecoCompra());

            return cri.uniqueResult();

    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object o) {
        return o.toString();
    }

}
